package com.thymeleaf.demo.util.result;

import java.util.Optional;
import java.util.function.Predicate;

public enum ResultEnum {
    // 自定义返回的code和对应的msg
    UNKNOW_ERROR(-1, "未知错误"),
    SUCCESS(200, "请求成功"),
    REQUEST_MISS(404, "请求不存在"),
    REQUEST_ERROR(500, "请求错误"),
    PARAMS_ERROR(1000, "参数错误"),

    GOODS_MISS(2000, "商品缺失，请添加购买商品"),

    USERNAME_ERROR(3000, "用户名错误"),
    PWD_ERROR(3001, "密码错误"),
    CODE_MORE_ERROR(3002, "验证码发送频繁，请稍后重试"),
    CODE_TEN_MORE_ERROR(3003, "验证码发送频繁，请十分钟后重试"),
    CODE_ONE_MORE_ERROR(3004, "验证码发送频繁，请一分钟后重试"),
    CODE_MISS(3005, "验证码已过期"),
    CODE_ERROR(3006, "验证码错误"),
    USERNAME_MORE(3007, "用户名已存在"),
    LOGIN_ERROR(3009, "登陆失效，请重新登陆"),

    ATTR_ERROR(4000, "属性有误"),
    ;

    private Integer code;
    private String msg;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    ResultEnum(Integer code, String msg){
       this.code = code;
       this.msg = msg;
    }

    public static ResultEnum getResultByCode(Integer code){
//        Optional<ResultEnum> enmu = EnumUtil.getEnumObject(ResultEnum.class, (e) -> {e.getCode().equals(code)});
        Optional<ResultEnum> enmu = EnumUtil.getEnumObject(ResultEnum.class,
                new Predicate<ResultEnum>() {
                    @Override
                    public boolean test(ResultEnum e) {
                        return e.getCode().equals(code);
                    }
                });
        return enmu != null && enmu.isPresent() ? enmu.get() : UNKNOW_ERROR;
    }
}
